<?php

require_once './application/core/Log/IndexCore.php';
require_once './application/core/Log/logFilter.php';
require_once './application/core/Client/logSoap.php';
require_once './application/models/Tables/cache.php';
require_once './application/entity/log/log.php';
require_once './library/XML/xmlToObjArray.php';

class logCore extends IndexCore
{

    private $_ip;
    private $_model;
    private $_type;
    private $_filtre;
    private $_identity;
    private $_data;
    private $_obj;
    private $_proto;

    public function log()
    {
        $this->_ip = $this->_controller->getRequest()->getParam('ip');
        $this->_model = $this->_controller->getRequest()->getParam('model');
        $this->_type = $this->_controller->getRequest()->getParam('type');
        $this->_filtre = $this->_controller->getRequest()->getParam('filtre');
        $this->_identity = $this->_controller->getRequest()->getParam('identity');
        $this->_proto = $this->_controller->getRequest()->getParam('proto');
        if (stristr($this->_proto, "auto"))
            $this->_proto = "ssh,telnet";
        $this->_obj = new CoreBase($this->_controller);
        $this->_identity = $this->_obj->getUsernameBase($this->_identity);
        $this->_ip = $this->_obj->parseArrayIp($this->_ip);
        $params = array('Ip' => $this->_ip,
            'Service' => 'log',
            'Model' => $this->_model,
            'Type' => $this->_type,
            'Filtre' => $this->_filtre,
            'Identity' => $this->_identity,
            'Protocol' => $this->_proto);
        $c = new logSoap($this->_controller);
        $c->log($params, 'logresp');
        $this->MiseEnAttenteReponse(true, '/log/logresp', null);
    }

    public function logResp()
    {
        $cache = new cache();
        $rst = $cache->getInfo($this->_controller->_idClient, "logresp");
        if ($rst != false)
        {
            $cache->deleteInfo($this->_controller->_idClient, 'logresp', $rst[0]['Id']);
            $this->_obj = new CoreBase($this->_controller);
            $xml = $this->_obj->parseResp($rst[0]['Xml']);
            if (isset($xml['LOG'][0]['DATA'][0][0]))
                $this->_data = base64_decode($xml['LOG'][0]['DATA'][0][0]);
            $this->_data = $this->createDataLogResp($this->_data, $xml['FILTRE'][0][0]);
            $this->_obj = new logArrayData();
            $this->_obj->aoColumns = $this->_data[0];
            $this->_obj->aaData = $this->_data[1];
            $this->MiseEnAttenteReponse(false, '/log/logresp', $this->_obj);
            return;
        }
        else
            $this->MiseEnAttenteReponse(true, '/log/logresp', null);
    }

    //envoie le xml pour le visuel
    private function createDataLogResp($xml, $name)
    {
        $this->_obj = new logData($this->_controller);
        $this->_data = $this->_obj->parseFilterData($xml, $name);
        return $this->_data;
    }

}
?>
